#include "ShaderSettings.txt"
#include "common.h"
#include "mblur.h"

struct v2p
{
  float4 tc0: 		TEXCOORD0;	// Center
  float4 tc1: 		TEXCOORD1;	// LT 		 
  float4 tc2: 		TEXCOORD2;	// RB
  float4 tc3: 		TEXCOORD3;	// RT 
  float4 tc4: 		TEXCOORD4;	// LB
  float4 tc5:		TEXCOORD5;	// Left	/ Right	
  float4 tc6:		TEXCOORD6;	// Top  / Bottom 
};
uniform sampler2D s_distort;
uniform half4 e_barrier;
uniform half4 e_weights;
uniform half4 e_kernel;
#include "blur.h"
#include "nightvision.h"
#define EPSDEPTH 0.001
half4 main(v2p I):COLOR {
#ifdef USE_DISTORT
half depth=tex2D(s_position,I.tc0).z;
half4 distort=tex2D(s_distort, I.tc0);
half2 offset=(distort.xy-.5h)*def_distort;
float2 center=I.tc0+offset;
half depth_x=tex2D (s_position,center).z;
if ((depth_x+EPSDEPTH)<depth) center=I.tc0;
#else
float2 center=I.tc0;
#endif
half3 img=tex2D(s_image,center);
half4 bloom=tex2D(s_bloom,center);
#ifdef IMPROVED_MBLUR
float z=tex2D(s_position,I.tc0).z;
if (z>START_DIST && z<FINAL_DIST) {img=mblur(center,tex2D(s_position,I.tc0),img.rgb);}
else { img=img.rgb;}
#else
img=mblur(center,tex2D(s_position,I.tc0),img.rgb);
#endif
#ifdef USE_DISTORT
half3 blurred=bloom*def_hdr;
img=lerp(img,blurred,distort.z);
#endif
float2 cent=I.tc0;
float dep=tex2D(s_position,cent).z;
float fov = e_weights.y * 1000;

float mycof=ZOOM_MAXCOF;
float myMINDIST = ZOOM_MINDIST;
float myMAXDIST = ZOOM_MAXDIST;
float myNEARMAXDIST = MAXDIST;
float myMAXNEAR = MAXNEAR;
float myMINNEAR = MINNEAR;
float targetDist = tex2D(s_position,ZOOM_CENTER).z; // e_weights.x * 1000;
if (targetDist <= myMAXNEAR) myMAXNEAR = targetDist;


half4 _P=tex2Dproj(s_position,I.tc0);

float px=_P.x / _P.z;
float py=_P.y / _P.z;

float center_dist = sqrt(pow(px, 2) + pow(py, 2));	// absolute distance from screen center
float dist_factor = center_dist * ZOOM_FACTOR * 1000 / fov; // weighted distance factor

if (dep < myMAXNEAR && dep > myMINNEAR) // && targetDist > myMAXNEAR)
{
	dep = (1 - (dep-myMINNEAR)/(myMAXNEAR-myMINNEAR) ) * myNEARMAXDIST;
	mycof = MAXCOF_NEAR;
}
else
	dep = dist_factor;

float blur=pow(saturate((dep-myMINDIST)/(myMAXDIST-myMINDIST)),2);

if (dep < ZOOM_BARRIER) // debug only
	blur = 0;
	
float3 sum=tex2D(s_image,cent).rgb;
float contrib=1.h;

#ifdef DOF_CLASSIC_ZOOM // use classic blur algorithm
float dof_classic_weight = 1;
float2 scale=float2(.5f/1024.f,.5f/768.f)*mycof*blur * dof_classic_weight;
float2 o [12];
o[0]=float2(-0.326212f,-0.405810f)*scale;
o[1]=float2(-0.840144f,-0.073580f)*scale;
o[2]=float2(-0.695914f,0.457137f)*scale;
o[3]=float2(-0.203345f,0.620716f)*scale;
o[4]=float2(0.962340f,-0.194983f)*scale;
o[5]=float2(0.473434f,-0.480026f)*scale;
o[6]=float2(0.519456f,0.767022f)*scale;
o[7]=float2(0.185461f,-0.893124f)*scale;
o[8]=float2(0.507431f,0.064425f)*scale;
o[9]=float2(0.896420f,0.412458f)*scale;
o[10]=float2(-0.321940f,-0.932615f)*scale;
o[11]=float2(-0.791559f,-0.597710f)*scale;
for (int i=0;i<DOF_QUALITY;i++)
{
	float2 tap=cent+o[i];
	float3 tap_color=tex2D(s_image,tap).rgb;
	sum+=tap_color;
	contrib++;
}
#else // use gaussian blur algorithm
float total_blur=mycof*blur*DOF_CORRECTION;
float f=0.f;
float inc=total_blur/DOF_QUALITY;
for (int i=0;i<DOF_QUALITY;i++){sum+=simpleblur(I,f);contrib++;f+=inc;}
#endif

#ifdef CONTRAST_FILTER
float3 highcontrast=img.xyz*img.xyz;
img.xyz=lerp(img.xyz,highcontrast,CONTRAST_FILTER_COEF);
#endif
#ifdef DEPTH_OF_FIELD
	#ifdef NV
	if (e_barrier.x > 0.f) // show NV only when red color amount > 0
		return nv(I, combine_bloom(img+(sum/contrib),bloom)-tex2D(s_image,I.tc0), float3(e_barrier.x, e_barrier.y, e_barrier.z), false);
	else
	#endif
return combine_bloom(img+(sum/contrib),bloom)-tex2D(s_image,I.tc0);
#else
return combine_bloom(img,bloom);
#endif 
}